package com.example.shell.model.entity;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.example.shell.util.LocalTimeDifference;
import lombok.Data;

import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Data
public class AttendanceRecord {
    private String sEmployeeCode;
    private String dDate;
    private String F1;
    private String F2;
    private String F3;
    private String F4;
    private String F5;
    private String F6;
    private int enLongIndex;
    private boolean enLongColumn;

    private String start;
    private String end;

    private String status;
    private String danger = "";

    private static final LocalTime split = LocalTime.of(11, 59);

    public void fill() {
        List<LocalTime> dates = new ArrayList<>();
        parseTime(F1, dates);
        parseTime(F2, dates);
        parseTime(F3, dates);
        parseTime(F4, dates);
        parseTime(F5, dates);
        parseTime(F6, dates);
        Collections.sort(dates);
        getMinOrMaxBySplit(dates);
        fillStatus(dates);
        fillDanger(dates);

        // 格式化一下日期
        if (StrUtil.isNotBlank(this.getDDate())){
            String[] sp = this.getDDate().split("T");
            this.setDDate(sp.length >0?sp[0]:"");
        }

    }

    private void parseTime(String str, List<LocalTime> dates) {
        if (StrUtil.isNotBlank(str)) {
            String[] time = str.split(":");
            dates.add(LocalTime.of(Integer.parseInt(time[0]), Integer.parseInt(time[1])));
        }
    }

    private void getMinOrMaxBySplit(List<LocalTime> dates) {
        if (CollectionUtil.isNotEmpty(dates)) {
            // 开始
            if (dates.get(0).isBefore(split)) {
                this.start = dates.get(0).getHour() + ":" + dates.get(0).getMinute();
            }
            if (!dates.get(dates.size()-1).isBefore(split)) {
                this.end = dates.get(dates.size()-1).getHour() + ":" + dates.get(dates.size()-1).getMinute();
            }
        }
    }

    private void fillStatus(List<LocalTime> dates){
        if (dates.size() <= 1){
            status = "X";
            return;
        }
        long l = LocalTimeDifference.differenceHours(dates.get(0), dates.get(dates.size()-1));
        if (l >= 9){
            status = "V";
        }else {
            status = "X";
        }
    }

    private void fillDanger(List<LocalTime> dates){
        if (dates.size() == 1){
            danger = "Q";
        }else if (dates.size() > 1){
            long l = LocalTimeDifference.differenceHours(dates.get(0), dates.get(dates.size()-1));
            if (l < 9){
                danger = "Q";
            }
        }

    }
}